<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GshowController extends Controller
{
    //
    public function show($id,Request $request)
    {
        //会话跟踪
        $user = $request->session()->get('user');
        //查询服装
        $pid = DB::table('bro_cate') -> where('id',$id) -> first() -> pid;
        
        $cate = DB::table('bro_cate') -> where('id',$pid) -> first() -> catename;

        $fz = '服装';
        //推广热卖
        $tgrm = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) ->take(3)->where('tuijian',1)->get();
        //推广商品
        $tgsp = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) ->take(2)->where('tuijian',0)->get();
        //达人选购
        $drxg = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) ->take(2)->where('status',1)->get();
        if($cate == $fz){
            $data = DB::table('bro_brand') ->get();
            $b = DB::table('bro_cate') -> where('pid',$id) ->get();

            foreach($b as $a){
                $b = $a -> id;
                $g[] = DB::table('bro_goods') ->where('category_id','=',$b) -> get();
            }
            $gs = [];
            foreach($g as $c){
                foreach($c as $val)
                {
                    $gs[] = $val;
                }
            }
            $goods= $gs;

            $brand = DB::table('bro_cate') -> where('id','=',$id) -> first() -> catename;
            $low = DB::table('bro_goods') -> where('price','>',200) -> get();
            $cate = DB::table('bro_cate') -> where('pid','=',1) -> get();
            $title = $brand;
        }else{

            $data = DB::table('bro_brand') ->get();
            $goods = DB::table('bro_goods') -> where('category_id','=',$id) -> get();
//            dd($goods);
            $brand = DB::table('bro_cate') -> where('id','=',$id) -> first() -> catename;
            $low = DB::table('bro_goods') -> where('price','>',200) -> get();
            $cate = DB::table('bro_cate') -> where('pid','=',1) -> get();

            $title = $brand;
        }
        $type = DB::table('bro_cate') -> where('pid',1) -> get();
        // $id = $type[0] -> id;
        //获取各类别的小类别
        foreach($type as $v)
        {
            $id  = $v -> id;
            // $id = $type[$i] -> id;
            // dd($v);
            $typechild = DB::table('bro_cate') -> where('pid',$id) -> get();
            // dd($typechild);
            $typechilds[] = $typechild;
        }
        $brands = DB::table('bro_brand') -> where('status','=',0) -> get();
        return view('home.gshow.gshow',['title' => $title,'data' => $data,'goods' => $goods,'tgsp' => $tgsp,'drxg'=>$drxg,'tgrm'=>$tgrm,'low' => $low,'cate' => $cate,'type'=>$type,'typechilds'=>$typechilds,'brands'=>$brands,'user' => $user]);
    }
    //
    public function show1($id,Request $request)
    {
        //会话跟踪
        $user = $request->session()->get('user');
        $type = DB::table('bro_cate') -> where('pid',1) -> get();
        $data = DB::table('bro_brand') ->get();
        $goods = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) -> where('brand','=',$id) -> get();

        $title = $goods[0] -> brand.'专柜';
        $good = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) ->get();
        $low = DB::table('bro_goods')  -> orderBy(DB::raw('RAND()')) -> where('price','<',200) -> get();
        //推广热卖
        $tgrm = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) ->take(3)->where('tuijian',1)->get();
        //推广商品
        $tgsp = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) ->take(3)->where('tuijian',0)->get();
        //达人选购
        $drxg = DB::table('bro_goods') -> orderBy(DB::raw('RAND()')) ->take(3)->where('status',1)->get();
        foreach($type as $v)
        {
            $id  = $v -> id;
            // $id = $type[$i] -> id;
            // dd($v);
            $typechild = DB::table('bro_cate') -> where('pid',$id) -> get();
            // dd($typechild);
            $typechilds[] = $typechild;
        }
        $brands = DB::table('bro_brand') -> where('status','=',0) -> get();
        return view('home.gshow.gshow',['title' => $title,'data' => $data,'goods' => $goods,'good' => $good,'low' => $low,'type'=>$type,'typechilds'=>$typechilds,'brands'=>$brands,'tgsp' => $tgsp,'drxg'=>$drxg,'tgrm'=>$tgrm,'user'=>$user]);
    }
    //商品搜索
    public function search(Request $request)
    {
        //会话跟踪
        $user = $request->session()->get('user');

        $data = DB::table('bro_brand') ->get();
        $goods = DB::table('bro_goods') ->where(function($query) use ($request){
            $query -> where('goods_name','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',100));
//        dd($goods);
        $type = DB::table('bro_cate') -> where('pid',1) -> get();
        $good = DB::table('bro_goods') ->get();
        $low = DB::table('bro_goods') -> where('price','<',200) -> get();
        //推广热卖
        $tgrm = DB::table('bro_goods')->take(3)->where('tuijian',1)->get();
        //推广商品
        $tgsp = DB::table('bro_goods')->take(3)->where('tuijian',0)->get();
        //达人选购
        $drxg = DB::table('bro_goods')->take(3)->where('status',1)->get();
        $brands = DB::table('bro_brand') -> where('status','=',0) -> get();
        $title = '京东';
        foreach($type as $v)
        {
            $id  = $v -> id;
            // $id = $type[$i] -> id;
            // dd($v);
            $typechild = DB::table('bro_cate') -> where('pid',$id) -> get();
            // dd($typechild);
            $typechilds[] = $typechild;
        }
        return view('home.gshow.gshow',['title' => $title,'goods' => $goods,'good' => $good,'low' => $low,'brands'=>$brands,'tgsp' => $tgsp,'drxg'=>$drxg,'tgrm'=>$tgrm,'user',$user,'type'=>$type,'typechilds'=>$typechilds,'data' => $data]);
    }
}
